package benlai_miniprogram.benlaiApi;

import benlai_miniprogram.HttpGlobal;

import benlai_miniprogram.JdbcLink;
import benlai_miniprogram.SqlAssert;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import ocg.common.re.Assertion;
import ocg.common.re.feed.Feed;
import ocg.http.httpcore.Http;
import ocg.http.httpcore.HttpFactory;
import ocg.http.httpcore.HttpModel;
import ocg.http.httpcore.HttpResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Map;

/**
 * Created by chenbo on 2018/5/10.
 */
public class Home{

    public static JSONArray value;
    public static Logger logger = LogManager.getLogger ( Home.class );

    public static void home(){

        HttpModel model = Http.get ( HttpGlobal.Path.Home_index )
                .parameters ( HttpGlobal.getParameters () );

        HttpResponse response = HttpFactory.factory ( model );

        JSONObject res = response.jsonBody ();

        int code = res.getIntValue ( "code" );

        Assertion.isEquals ( code , 0 ,"首页接口请求成功！" );

        if (code == 0 ){

            value = res.getJSONArray ( "value" );

            if (JdbcLink.isDB_Check ()) {

                int index = value.size ();

                String master_sql = "SELECT [SysNo]\n" +
                        "      ,[Name]\n" +
                        "      ,[WebSiteSysNo]\n" +
                        "      ,[LotType]\n" +
                        "      ,[IsHomePage]\n" +
                        "  FROM [WeChatApplet].[dbo].[MiniAppHomePage_Master]\n" +
                        "  WHERE [WebSiteSysNo]="+HttpGlobal.getWebSiteSysNo ()+" AND Status= 1 AND IsHomePage = 1 order by OrderIndex";

                Feed master = SqlAssert.feed ( master_sql );

                logger.debug ( "首页-master\n" + master );

                if ( Assertion.isEquals ( master.size () , index , "首页广告位数量相同：" + index ) ) {
                    if (index>0) {

                        for (int i = 0; i < index; i++) {
                            JSONObject home_master = value.getJSONObject ( i );
                            int master_sysno = home_master.getIntValue ( "sysNo" );
                            int master_type = home_master.getIntValue ( "type" );
                            String master_title = home_master.getString ( "title" );
                            JSONArray items = home_master.getJSONArray ( "items" );

                            Map<String,Object> map_DB = master.feeder ( i );

                            Assertion.isEquals ( map_DB.get ( "Name" ).toString ()
                            , master_title , "检查title相同！"+ master_title);

                            String item_sql = "SELECT [SysNo]\n" +
                                    "      ,[ItemType]\n" +
                                    "      ,[HomeImage]\n" +
                                    "  FROM [WeChatApplet].[dbo].[MiniAppHomePage_Item]\n" +
                                    "  WHERE MasterSysNo = "+ master_sysno;

                            Feed item = SqlAssert.feed ( item_sql );

                            if ( Assertion.isEquals ( item.size () , items.size () , "广告位item返回相同:" + master_sysno )) {
                                if (items.size ()>0) {
                                    for (int i1 = 0; i1 < items.size (); i1++) {
                                        JSONObject item_show = items.getJSONObject ( i1 );
                                        int item_sysno = item_show.getIntValue ( "sysNo" );
                                        String imageUrl = item_show.getString ( "imageUrl" );
                                        int target = item_show.getIntValue ( "target" );


                                        Assertion.isContains ( imageUrl
                                                , item.feeder ( i1 ).get ( "HomeImage" ).toString ()
                                                , "图片相同！");

                                        String param_sql = "";
                                        if (target == 1) {
                                            JSONObject parameter = item_show.getJSONObject ( "parameter" );
                                            param_sql = "SELECT \n" +
                                                    "      [ProductBasicSysNo]\n" +
                                                    "      ,[ChannelSysNo]\n" +
                                                    "      ,[SaleChannelSysNo]\n" +
                                                    "  FROM [WeChatApplet].[dbo].[MiniAppHomePage_Content]\n" +
                                                    "  WHERE ItemSysNo = "+item_sysno;

                                            Feed param = SqlAssert.feed ( param_sql );

                                            Assertion.isEquals ( parameter.getIntValue ( "productBasicSysNo" )
                                            ,Integer.valueOf ( param.feeder ( 0 ).get ( "ProductBasicSysNo" ).toString () )
                                            ,"商品主码相同:" + parameter.getIntValue ( "productBasicSysNo" ));

                                        }else if (target==3){
                                            String parameter = item_show.getString ( "parameter" );
                                            param_sql ="SELECT\n" +
                                                    "      [Value]\n" +
                                                    "  FROM [WeChatApplet].[dbo].[MiniAppHomePage_Content]\n" +
                                                    "  WHERE ITEMSYSNO = "+item_sysno;

                                            Feed param = SqlAssert.feed ( param_sql );

                                            Assertion.isEquals ( parameter , param.feeder ( 0 ).get ( "Value" ).toString () ,"页面地址相同" );
                                        }else if (target==2){
                                            Assertion.isNull ( item_show.get ( "parameter" ) , "商详广告位" );
                                        }
                                    }
                                }
                            }

                        }

                    }else {
                        logger.info ( "该站点数据为空：" + HttpGlobal.getWebSiteSysNo () );
                    }
                }
            }
        }
    }
}
